/**
 * May 21, 2014 8:34:08 AM
 */
package elitech.vietnam.copyweatherfashion.fragments.settings;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.dialog.ProgressDialogFragment;
import elitech.vietnam.copyweatherfashion.utilities.Utilities;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class ChangePasswordFragment extends Fragment implements View.OnClickListener {

	MainActivity mActivity;
	
	TextView mTxtError;
	EditText mEdtOldPass, mEdtNewPass, mEdtCfrPass;
	Button mBtnSubmit;
	
	public ChangePasswordFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_changpass, container, false);
		
		mTxtError = (TextView) view.findViewById(R.id.changepass_txtError);
		mEdtOldPass = (EditText) view.findViewById(R.id.changepass_edtOldPass);
		mEdtNewPass = (EditText) view.findViewById(R.id.changepass_edtNewPass);
		mEdtCfrPass = (EditText) view.findViewById(R.id.changepass_edtCfrPass);
		mBtnSubmit = (Button) view.findViewById(R.id.changepass_btnSubmit);
		
		mBtnSubmit.setOnClickListener(this);
		
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.changepass_btnSubmit:
			InputMethodManager inputManager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
			View view = mActivity.getCurrentFocus();
		    if (view != null)
		    	inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			if (mEdtOldPass.getText().toString().length() == 0) {
				mTxtError.setText(getString(R.string.setting_old_password_request));
				mEdtOldPass.requestFocus();
			} else if (mEdtNewPass.getText().toString().length() == 0) {
				mTxtError.setText(getString(R.string.setting_new_password_request));
				mEdtNewPass.requestFocus();
			} else if (mEdtCfrPass.getText().toString().length() == 0) {
				mTxtError.setText(getString(R.string.setting_confirm_password_request));
				mEdtCfrPass.requestFocus();
			} else if (!mEdtNewPass.getText().toString().equals(mEdtCfrPass.getText().toString())) {
				mTxtError.setText(getString(R.string.setting_confirm_password_false));
				mEdtCfrPass.requestFocus();
			} else if (!Utilities.md5(mEdtOldPass.getText().toString()).equals(mActivity.mUser.Password)) {
				mTxtError.setText(getString(R.string.setting_old_password_false));
				mEdtOldPass.requestFocus();
			} else if (mEdtOldPass.getText().toString().equals(mEdtNewPass.getText().toString())) {
				mTxtError.setText(getString(R.string.setting_old_password_like_new_password));
				mEdtOldPass.requestFocus();
			} else {
				new UpdatePassword().execute();
			}
			break;

		default:
			break;
		}
	}
	
	class UpdatePassword extends AsyncTask<Integer, Integer, Boolean> {
		ProgressDialogFragment mDialog;
		@Override
		protected void onPreExecute() {
			mDialog = new ProgressDialogFragment.Builder()
			.setMessage(getString(R.string.please_wait))
			.setTitle(getString(R.string.loading))
			.build();
			mDialog.show(getFragmentManager());
		}
		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				return new Service().changePassword(mActivity.mUser.Id, mEdtNewPass.getText().toString()) > 0;
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
				return false;
			}
		}
		@Override
		protected void onPostExecute(Boolean result) {
			mDialog.dismiss();
			if (!isVisible())
				return;
			if (!result)
				mTxtError.setText(R.string.setting_change_password_fail);
			else {
				Toast.makeText(mActivity, R.string.setting_change_password_success, Toast.LENGTH_SHORT).show();
				mActivity.mUser.Password = Utilities.md5(mEdtNewPass.getText().toString());
				mActivity.onBackPressed();
			}
		}
	}
}
